<?php

namespace App\Services\Plaza;

use App\Models\AreaJobPositionModel;
use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use App\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;

class PlazaService
{
    public function list(Authenticatable|User|null $user)
    {
        return PlazaModel::query()
            ->with(['jobPositionInfo'])
            ->where('cur_status', 1)
            ->whereHas('jobPositionInfo', function (Builder $query) use ($user) {
                $query->validPositions();
                //                if ($user) {
                //                    //登录用户做年龄性别限制
                //                    $age = $user->age();
                //                    if ($age != -1) {
                //                        $query->whereRaw("((min_age = 0) or (min_age <= $age))")->whereRaw("((max_age =0) or (max_age >= $age))");
                //                    }
                //                    //登录用户做性别限制
                //                    if ($user->gender > 0) {
                //                        $query->whereRaw("((gender_requirement = 3) or (gender_requirement = $user->gender))");
                //                    }
                //                }
            })
            ->orWhere(function (Builder $query) {
                $query->where('cur_type', PlazaModel::TYPE_AREA)->where('cur_status', 1);
            })
            ->orderBy('sort')
            ->paginate();
        // })->orWhere('cur_type', PlazaModel::TYPE_AREA)
        // ->orderBy('sort')
        // ->paginate();
    }

    /**
     * @param Authenticatable|User|null $user
     * @param int $plaza_id
     * @return LengthAwarePaginator
     */
    public function areaList(Authenticatable|User|null $user, int $plaza_id, int $platform_id)
    {
        $idArray = AreaJobPositionModel::query()->orderBy('sort')->where('plaza_id', $plaza_id)->pluck('job_position_id')->toArray();
        $now = now();
        $sortString =  implode(',', $idArray);
        $query = JobpositionModel::query()
            ->with([
                'categories',
                'company',
            ])
            ->whereIn('id', $idArray)->validPositions()
            ->orderByRaw("field(`id`,$sortString)");
        if ($platform_id) {
            $query->whereHas('platforms', function ($query) use ($platform_id) {
                $query->where('platform_id', $platform_id);
            });
        }

        //        if ($user) {
        //            //登录用户做年龄性别限制
        //            $age = $user->age();
        //            if ($age != -1) {
        //                $query->whereRaw("((min_age = 0) or (min_age <= $age))")->whereRaw("((max_age =0) or (max_age >= $age))");
        //            }
        //            //登录用户做性别限制
        //            if ($user->gender > 0) {
        //                $query->whereRaw("((gender_requirement = 3) or (gender_requirement = $user->gender))");
        //            }
        //        }
        return $query->paginate();
    }
}
